<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2017/9/26 0026
 * Time: 上午 11:16
 */
namespace app\api\controller;

use think\Db;
use think\Exception;
use think\Request;
use think\Validate;
use think\controller\Rest;
use app\common\model\Engineers as EngineersDao;

class Engineers extends RestUserBase {

    protected $eUserInfo;
    protected $engineersDao;
    public function __construct()
    {
        parent::__construct();
        $this->eUserInfo = $this->user;
        $this->engineersDao =  new EngineersDao();
    }

    //获取工程人员信息
    public function getUserInfo(){
        $userInfo = $this->user;
        unset($userInfo['engineers_password']);
        unset($userInfo['engineers_slat']);
        $userInfo['avatar_img'] =config('API_IMAGES_PATH').$userInfo['avatar_img'];
        $this->success('成功',$userInfo);
    }

    // 用户密码重置
    public function passwordReset()
    {
        if($this->method != 'post'){
            $this->error("非法请求");
        }
        $validate = new Validate([
            'opd'          => 'require',
            'npd'          => 'require',
            'rnpd'         => 'require'
        ]);
        $validate->message([
            'opd.require'          => '请输入原始密码!',
            'npd.require'          => '请输入新密码!',
            'rnpd.require'         => '请重复输入新密码!'
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        if($data['npd']!==$data['rnpd']){
            $this->error('二次密码不一致,请重新输入!');
        }
        $ret = cms_compare_password($data['opd'],$this->eUserInfo['engineers_password'],$this->eUserInfo['engineers_slat']);
        if(!$ret){
            $this->error('原始密码错误,请重复输入!');
        }
        $where['engineers_id'] = $this->eUserInfo['engineers_id'];
        $udata['engineers_password'] = compare_password($data['npd'],$this->eUserInfo['engineers_slat']);
        $uret = $this->engineersDao->where($where)->update($udata);
        if($uret){
            //清理登录token
            $dwhere['user_id'] = $this->eUserInfo['engineers_id'];
            Db::name('user_token')->where($dwhere)->delete();
            $this->success("密码重置成功,请使用新密码登录!");
        }else{
            $this->error('密码重置失败');
        }
    }

    //修改头像
    public function headImgReset(){
        if($this->method != 'post'){
            $this->error("非法请求");
        }
        $file = $this->request->file('himg');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                Db::startTrans();
                try{
                    $filename = $info->getSaveName();
                    $data['url'] = $filename;
                    $uwhere['engineers_id'] = $this->eUserInfo['engineers_id'];
                    $udata['avatar_img'] = $data['url'];
                    $this->engineersDao->where($uwhere)->update($udata);
                    Db::commit();
                    $this->success('成功',$data);
                }catch (Exception $e){
                    Db::rollback();
                    $this->error('失败',$e);
                }
            }else{
                // 上传失败获取错误信息
                $this->error('失败');
            }
        }

    }
}